/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.fraction;

import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.util.DataTransferStation;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.global.resource.WrongTypeException;

import java.lang.invoke.WrongMethodTypeException;

/**
 * BaseFraction
 */
public abstract class BaseFraction extends Fraction {

    boolean isActive = false;
    DataTransferStation.PageHelper dataTransferHelper = new DataTransferStation.PageHelper(this);
    boolean userVisibleHint = true;
    private Component rootView;

    Object parent;

    /**
     * getLayoutResId
     * @return int
     */
    public abstract int getLayoutResId();

    /**
     * getVisibleToUser
     * @return boolean
     */
    public boolean getVisibleToUser() {
        return isActive && userVisibleHint;
    }

    public BaseFraction(Object parent)  {
        if(parent instanceof Ability ||parent instanceof Fraction) {
            this.parent = parent;
        }else{
            throw new WrongMethodTypeException("请传入一个父类的ability或者是fraction");
        }
    }

    public Object getParent() {
        return parent;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        dataTransferHelper.onCreate(intent);
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return onCreateView(scatter, container, intent);
    }

    /**
     * onCreateView
     * @param scatter
     * @param container
     * @param intent
     * @return Component
     */
    protected Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        if (getLayoutResId() == 0) {
            new NullPointerException("请填充一个初始布局");
        }
        rootView = scatter.parse(getLayoutResId(), null, false);
        rootView.getLayoutConfig().width=ComponentContainer.LayoutConfig.MATCH_PARENT;
        rootView.getLayoutConfig().height=ComponentContainer.LayoutConfig.MATCH_PARENT;
        onViewCreated(rootView, intent);
        initView(rootView);
        return rootView;
    }

    /**
     * getView
     * @return Component
     */
    public Component getView() {
        return rootView;
    }

    /**
     * initView
     * @param parent
     */
    public abstract void initView(Component parent);

    /**
     * onViewCreated
     * @param component
     * @param intent
     */
    protected void onViewCreated(Component component, Intent intent) {
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        isActive = false;
        if (userVisibleHint) {
            onUserVisibleChanged(false);
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
        isActive = true;
        if (userVisibleHint) {
            onUserVisibleChanged(true);
        }
    }

    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isActive) {
            onUserVisibleChanged(isVisibleToUser);
        }
    }

    /**
     * onUserVisibleChanged
     * @param isVisibleToUser
     */
    protected void onUserVisibleChanged(boolean isVisibleToUser) {

    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
    }

    @Override
    protected void onStop() {
        super.onStop();
        dataTransferHelper.onDestroy();
    }


}
